#include<iostream>
#include<cstring>
#include<cstdio>
#include<unistd.h>
#include<cstdlib>
using namespace std;


int main(){


    int pipefd[2] = {0};
    int n = pipe(pipefd);
    if(n<0){
        cout<<"perror file"<<endl;
        return 1;
    }

    pid_t id=fork();


    if(id==0){

    close(pipefd[1]);
    char buffer[1024];
    n = read(pipefd[0],buffer,sizeof(buffer) - 1);
    if(n > 0){
        buffer[n] = '\0';
        cout << "i am child,father give me message: " << buffer <<endl;
    }
    else if(n == 0){
         cout << "i am childr,i read to the end" << endl;
    }
    close(pipefd[0]); 
    }
       //父进程负责写入
        close(pipefd[0]);
        string tmp="i am father";
        char buffer[1024];
        snprintf(buffer,sizeof(buffer),"%s",tmp.c_str());
        write(pipefd[1],buffer,strlen(buffer));
        close(pipefd[1]);
        exit(0);


    return 0;
}